package org.example.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.pojo.entity.WmNews;
import org.example.pojo.req.AuditReq;
import org.example.pojo.vo.WmNewsVoPage;
import org.example.mapper.WmNewsMapper;
import org.example.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 妖妖零
 * @description 针对表【wm_news(自媒体图文内容信息表)】的数据库操作Service实现
 * @createDate 2024-06-06 16:58:27
 */
@Service
public class WmNewsServiceImpl extends ServiceImpl<WmNewsMapper, WmNews>
        implements WmNewsService {

    @Autowired
    private WmNewsMapper wmNewsMapper;

    @Override
    public Page getArticleList(Integer pages, Integer size, String title, String name, String startTime, String endTime) {
        List<WmNewsVoPage> wmNewsVoPage = wmNewsMapper.getArticleList(title, name, startTime, endTime, pages, size);
        Page page = new Page(pages, size);
        Long count = baseMapper.selectCount(null);
        page.setTotal(count);
        page.setRecords(wmNewsVoPage);
//        // 当前页第一条数据在List中的位置
//        int start = (int) ((page.getCurrent() - 1) * page.getSize());
//        // 当前页最后一条数据在List中的位置
//        int end = (int) ((start + page.getSize()) > wmNewsVoPage.size() ? wmNewsVoPage.size() : (page.getSize() * page.getCurrent()));
//        page.setRecords(new ArrayList<>());
//        page.setTotal(wmNewsVoPage.size());
//        if (page.getSize() * (page.getCurrent() - 1) <= page.getTotal()) {
//            // 分隔列表 当前页存在数据时 设置
//            page.setRecords(wmNewsVoPage.subList(start, end));
//        }

        return page;

    }

    @Override
    public void audit(AuditReq req) {
        WmNews wmNews = new WmNews();
        wmNews.setId(Integer.valueOf(req.getNewsId()));
        if ("PASS".equals(req.getType())) {
            wmNews.setStatus(1);
        } else {
            wmNews.setStatus(2);
        }
        wmNews.setReason(req.getAdvice());

        baseMapper.updateById(wmNews);
    }
}




